fin = File.new(ARGV[0] ? ARGV[0] : "ratings.txt", "r")
htmlRatings = File.new("ratings.html", "w")

if(ARGV[1])
	ratingChanges = File.new(ARGV[1] , "r")
	changes = {}
	while(line = ratingChanges.gets)	
		if(/^(.+?)\s+([\d.-]+)/.match(line))
			changes[$1] = $2;		
		end
	end
end

pos = {}
if(ARGV[2])
	puts "reading old ratings..."
	prevRatings = File.new(ARGV[2] , "r")	
	num = 1
	while(line = prevRatings.gets)
		if(/^(.+?)\s+([\d.]+)/.match(line))
			pos[$1] = num;		
			num+=1;
		end
	end
end

downArrow = "http://us.i1.yimg.com/us.yimg.com/i/us/fi/03rd/down_r.gif";
upArrow = "http://us.i1.yimg.com/us.yimg.com/i/us/fi/03rd/up_g.gif";

$i = 1;
while(line = fin.gets)	
	if(/^(.+?)\s+([\d.]+)/.match(line))			
	    changesStr = "";
		if(changes[$1])
			ratingChange = changes[$1]
			color = "red"
			if(ratingChange=="0")
				color = "cyan"
			end 
			if(ratingChange[0]!=45 && ratingChange!="0")
				color = "green"
				ratingChange = "+" + ratingChange
			end
			changesStr = " [b][color="+color+"]"+ratingChange+"[/color][/b]";
		end
		
		if(pos[$1])
			posChng = pos[$1]-$i;
			if(posChng != 0)
				changesStr += " [img]" + (posChng > 0 ? upArrow : downArrow) + "[/img]" + posChng.abs.to_s;
			end
		end
		
		htmlRatings << $i.to_s << " "*(3 - $i.to_s.length) << $1 << " "*(20 - $1.length) << $2.to_f.round << changesStr << "\n" if($2.to_f > 0);
		$i+=1;
	end
end
fin.close
htmlRatings.close
